package com.qqtech.qquan.community.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.community.dao.CommunityJoinDao;
import com.qqtech.qquan.community.enums.CommunityJoinStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityJoin;
import com.qqtech.qquan.community.model.CommunityJoinQuery;
import com.qqtech.qquan.community.model.CommunityJoinVo;
import com.qqtech.qquan.hi.service.HiCommunityService;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserBaseQuery;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Service
public class CommunityJoinServiceImpl extends BaseServiceImpl<CommunityJoin> implements CommunityJoinService {
	public static final Logger log = LoggerFactory.getLogger(CommunityJoinServiceImpl.class);

	@Resource
	private CommunityJoinDao communityJoinDao;

	@Resource
	private HiCommunityService hiCommunityService;

	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseDao<CommunityJoin> getBaseDao() {
		return communityJoinDao;
	}

	@Override
	public CommunityJoinVo getFirstOne(CommunityJoinQuery communityJoinQuery, PageOrder pageOrder) {
		Pageable pageKit = new PageKit(1, 1, pageOrder);
		List<CommunityJoinVo> list = communityJoinDao.queryList(communityJoinQuery, pageKit);
		return list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public boolean isJoinCommunity(Integer communityId, Integer userId) {
		UserBase userBase = userBaseService.getById(userId);
		if (null != userBase.getCommunityId() && communityId.intValue() == userBase.getCommunityId()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isNeighbor(Integer userId, Integer neighborId) {
		boolean isNeighbor = false;
		UserBase userBase = userBaseService.getById(userId);
		UserBase neighborUserBase = userBaseService.getById(neighborId);
		if (null != userBase.getCommunityId() && null != neighborUserBase.getCommunityId()
				&& userBase.getCommunityId().intValue() == neighborUserBase.getCommunityId()) {
			isNeighbor = true;
		}
		return isNeighbor;
	}

	@Override
	public List<Integer> queryMyCommuntiyIds(Integer userId) {
		if (null == userId) {
			return new ArrayList<Integer>();
		}
		return communityJoinDao.queryMyCommuntiyIds(userId);
	}

	@Override
	public List<Integer> queryOurCommunityIds(Integer userId, Integer neighborId) {
		UserBase userBase = userBaseService.getById(userId);
		UserBase neighborUserBase = userBaseService.getById(neighborId);
		if (null != userBase.getCommunityId() && null != neighborUserBase.getCommunityId()
				&& userBase.getCommunityId().intValue() == neighborUserBase.getCommunityId()) {
			List<Integer> list = new ArrayList<Integer>();
			list.add(userBase.getCommunityId());
			return list;
		}
		return null;
	}

	@Override
	public List<Integer> getNeighborUserIds(Integer userId) {
		UserBase userBase = userBaseService.getById(userId);
		if (null != userBase.getCommunityId()) {
			List<Integer> userIds = new ArrayList<Integer>();
			UserBaseQuery query = new UserBaseQuery();
			query.setCommunityId(userBase.getCommunityId());
			List<UserBaseVo> list = userBaseService.queryList(query);
			for (UserBaseVo userBaseVo : list) {
				userIds.add(userBaseVo.getId());
			}
			return userIds;
		}
		return new ArrayList<Integer>();
	}

	@Override
	public List<CommunityJoinVo> queryLeftBase(CommunityJoinQuery communityJoinQuery) {
		return communityJoinDao.queryLeftBase(communityJoinQuery);
	}

	@Override
	public int handleJoin(Integer userId, Integer communityId) {
		UserBase userBase = userBaseService.getById(userId);
		userBase.setCommunityId(communityId);
		userBaseService.updateById(userBase);

		CommunityJoinQuery query = new CommunityJoinQuery();
		query.setUserId(userId);
		query.setStatus(CommunityJoinStatusDictEnum.NORMAL.getCode());
		List<CommunityJoinVo> list = communityJoinDao.queryList(query);
		if (null != list && list.size() > 0) {
			// 退出之前加入的社区
			for (CommunityJoinVo communityJoinVo : list) {
				communityJoinVo.setStatus(CommunityJoinStatusDictEnum.QUIT.getCode());
				communityJoinVo.setQuitAt(TimeUtil.now());
				communityJoinDao.updateById(communityJoinVo);
			}
		}
		// 新增加入社区记录
		CommunityJoin communityJoin = new CommunityJoin();
		communityJoin.setUserId(userId);
		communityJoin.setCommunityId(communityId);
		communityJoin.setStatus(CommunityJoinStatusDictEnum.NORMAL.getCode());
		if (userBase.getIsOfficial() == null || YesNoEnum.YES.getCode() != userBase.getIsOfficial().intValue()) {
			// 荣誉系统奖励-ryz
			hiCommunityService.join(userId, communityId);
		}
		return communityJoinDao.insert(communityJoin);
	}

}